from django.core.paginator import Paginator
from django.shortcuts import render
from rest_framework.generics import RetrieveAPIView
from rest_framework.views import APIView
# from rest_framework.permissions import IsAdminUser
from rest_framework.response import Response

from order.models import Order
from .models import Area, House, HouseImage
from ihome.serialziers.homes import AreaSerialzier, HouseCreateSerializier, HouseListSerializers, HouseImageSerializers, HouseDetailsSerializers
from rest_framework.viewsets import ModelViewSet


# Create your views here.

class AreaView(APIView):
    """城区列表"""

    def get(self, request):
        # 获取房屋地址
        area = Area.objects.all()
        serializer = AreaSerialzier(area, many=True)

        return Response({"errno": "0", "errmsg": "获取成功", "data": serializer.data})


class HouseCreateView(ModelViewSet):
    """发布房源和房屋数据搜索功能"""
    queryset = House.objects.all()
    serializer_class = HouseCreateSerializier


    def get(self, request):
        aid = request.GET.get('aid')
        sd = request.GET.get('sd')
        ed = request.GET.get('ed')
        sk = request.GET.get('sk')
        page = request.GET.get('p')

        # 被预定的房间号 order.id

        orders1 = Order.objects.filter(begin_date__in=(sd, ed))
        orders2 = Order.objects.filter(end_date__in=(sd, ed))

        orders = orders2 | orders1

        house_list = []
        for order in orders:
            house_id = order.house.id
            house_list.append(house_id)

        house = House.objects.filter(area_id=aid)

        house_end = house.exclude(id__in=house_list)

        paginator = Paginator(house_end, 10)

        house_page = paginator.page(page)

        serializer = HouseCreateSerializier(house_page, many=True)
        data = {"house": serializer.data}
        return Response({'errno':0, 'errmsg': '请求成功', 'data':data})


class HouseListView(ModelViewSet):
    """获取房屋列表"""
    queryset = House.objects.all()
    serializer_class = HouseListSerializers

    def list(self, request, *args, **kwargs):
        queryset = self.filter_queryset(self.get_queryset())

        page = self.paginate_queryset(queryset)
        if page is not None:
            serializer = self.get_serializer(page, many=True)
            return self.get_paginated_response(serializer.data)

        serializer = self.get_serializer(queryset, many=True)
        data = {'house': serializer.data}
        return Response({"errno": "0", "errmsg": "ok", "data": data})


class HouseIndexView(APIView):
    """获取房屋首页图片"""

    def get(self, request):
        # 获取房屋地址
        houseimge = HouseImage.objects.all()
        serializers = HouseImageSerializers(houseimge, many=True)

        return Response({"errno": "0", "errmsg": "ok", "data": serializers.data})


class HouseDetailsView(RetrieveAPIView):
    """房屋详情页"""

    queryset = House.objects.all()
    serializer_class = HouseDetailsSerializers

    def retrieve(self, request, *args, **kwargs ):
        instance = self.get_object()
        serializer = self.get_serializer(instance)

        data = { "house" : serializer.data }
        user_id = data['house']['user_id']
        data['user_id'] = user_id

        return Response({
            "errmsg": "OK",
            "errno": "0",
            "data": data
        })




